package com.stephentuso.welcomeexample;

import com.stephentuso.welcome.WelcomeAbility;
import com.stephentuso.welcome.WelcomeHelper;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TableLayoutManager;
import ohos.agp.components.Text;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.utils.PacMap;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class MainAbility extends Ability {

    private static final int REQUEST_WELCOME_SCREEN_RESULT = 13;

    private WelcomeHelper sampleWelcomeScreen;

    private List<MainAbility.ScreenItem> welcomeScreens = new ArrayList<>();

    private ListContainer recyclerView;
    
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        recyclerView = (ListContainer) findComponentById(ResourceTable.Id_recycler_view);

        // The welcome screen for this app (only one that automatically shows)
        sampleWelcomeScreen = new WelcomeHelper(this, SampleWelcomeAbility.class);
        sampleWelcomeScreen.show(null);

        //List of welcome screens
        welcomeScreens.add(new ScreenItem(ResourceTable.String_title_sample, ResourceTable.String_description_sample, sampleWelcomeScreen, null));
        welcomeScreens.add(new ScreenItem(ResourceTable.String_title_button_bar, ResourceTable.String_description_button_bar, ButtonBarWelcomeAbility.class, REQUEST_WELCOME_SCREEN_RESULT));
        welcomeScreens.add(new ScreenItem(ResourceTable.String_title_included_pages, ResourceTable.String_description_included_pages, IncludedPagesWelcomeAbility.class));
        welcomeScreens.add(new ScreenItem(ResourceTable.String_title_single_button, ResourceTable.String_description_single_button, ButtonBarSingleWelcomeAbility.class));
        welcomeScreens.add(new ScreenItem(ResourceTable.String_title_indicator_only, ResourceTable.String_description_indicator_only, IndicatorOnlyWelcomeAbility.class));
        welcomeScreens.add(new ScreenItem(ResourceTable.String_title_custom_page, ResourceTable.String_description_custom_page, CustomPageWelcomeAbility.class));
        welcomeScreens.add(new ScreenItem(ResourceTable.String_title_no_skip, ResourceTable.String_description_no_skip, NoSkipWelcomeAbility.class, REQUEST_WELCOME_SCREEN_RESULT));
        welcomeScreens.add(new ScreenItem(ResourceTable.String_title_back_exit, ResourceTable.String_description_back_exit, BackExitWelcomeAbility.class));
        welcomeScreens.add(new ScreenItem(ResourceTable.String_title_previous_button, ResourceTable.String_description_previous_button, PreviousButtonWelcomeAbility.class));
        welcomeScreens.add(new ScreenItem(ResourceTable.String_title_default, ResourceTable.String_description_default, DefaultWelcomeAbility.class));

        TableLayoutManager layoutManager = new TableLayoutManager();
        layoutManager.setColumnCount(1);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemProvider(new Adapter());
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent data) {
        super.onAbilityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_WELCOME_SCREEN_RESULT) {

            if (resultCode == -1) {
                ToastDialog toastDialog = new ToastDialog(getApplicationContext());
                toastDialog.setText("Completed (RESULT_OK)");
                toastDialog.setAlignment(TextAlignment.BOTTOM);
                toastDialog.show();
            } else if (resultCode == 0) {
                ToastDialog toastDialog = new ToastDialog(getApplicationContext());
                toastDialog.setText("Canceled (RESULT_CANCELED)");
                toastDialog.setAlignment(TextAlignment.BOTTOM);
                toastDialog.show();
            }

        }

    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);

        // This is needed to prevent welcome screens from being
        // automatically shown multiple times

        // This is the only one needed because it is the only one that
        // is shown automatically. The others are only force shown.
        sampleWelcomeScreen.onSaveInstanceState(outState);
    }

    private void openUrlInBrowser(String urlString) {
        Operation operationBuilder = new Intent.OperationBuilder()
                .withUri(Uri.parse(urlString))
                .build();
        Intent intent = new Intent();
        intent.setOperation(operationBuilder);
        startAbility(intent);
    }

    private class Adapter extends BaseItemProvider {

        @Override
        public int getCount() {
            return welcomeScreens.size();
        }

        @Override
        public Object getItem(int i) {
            return welcomeScreens.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
            if (component == null) {
                component = LayoutScatter.getInstance(MainAbility.this).parse(ResourceTable.Layout_item_welcome_screen_list, null, false);
            }
            final ScreenItem item = welcomeScreens.get(i);

            Text title = (Text) component.findComponentById(ResourceTable.Id_title);
            Text description = (Text) component.findComponentById(ResourceTable.Id_description);
            Component button = component.findComponentById(ResourceTable.Id_button);

            title.setText(item.title);
            description.setText(item.description);
            button.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component v) {
                    if (item.requestCode != null) {
                        item.helper.forceShow(item.requestCode);
                    } else {
                        item.helper.forceShow();
                    }
                }
            });
            return component;
        }

    }

    /**
     * Data model for list of welcome screens
     * Title to show in list, welcome screen to show, request code
     */
    private class ScreenItem {

        String title;
        String description;
        WelcomeHelper helper;
        Integer requestCode;

        ScreenItem(int titleRes, int descriptionRes, Class<? extends WelcomeAbility> abilityClass) {
            this(titleRes, descriptionRes, abilityClass, null);
        }

        ScreenItem(int titleRes, int descriptionRes, Class<? extends WelcomeAbility> abilityClass, Integer requestCode) {
            this(titleRes, descriptionRes, new WelcomeHelper(MainAbility.this, abilityClass), requestCode);
        }

        ScreenItem(int titleRes, int descriptionRes, WelcomeHelper helper, Integer requestCode) {
            this.title = getString(titleRes);
            this.description = getString(descriptionRes);
            this.helper = helper;
            this.requestCode = requestCode;
        }

    }
}
